package DruidTest;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
 
public class TestMain {
 
	/**
	 * 测试方式： 每种数据源配置信息尽量相同，以求结果更加准确
	 * 每种数据源做10次、100次、500次、1000次、2000次、4000次、8000次查询操作 每种查询重复100次，查看100次执行时间的波动图
	 * 
	 * @param args
	 * @throws IOException
	 * @throws SQLException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws IOException, SQLException, InterruptedException {
		// 准备数据源
		TestDAO testDAO = new TestDAO();
		ITestDataSource testDataSource = null;
		{
			System.out.println("===========单线程执行===============");
			testDataSource = new TestDataSource();
			batchTestRunMillis(testDAO, testDataSource);
		}
	}
 
	/**
	 * @param testDAO
	 * @param testDataSource
	 * @throws SQLException
	 * @throws InterruptedException
	 */
	private static void batchTestRunMillis(TestDAO testDAO, ITestDataSource testDataSource) throws SQLException,
			InterruptedException {
		// testDataSource = new TestDataSourceByThread();
		// 查询次数
		int count = 100;
		long c3p0Millis = 0l, druidMillis = 0l, proxoolMillis = 0l, tomcatMillis = 0l;

		{// Druid数据源
			DataSource dataSource = AllDataSource.getDruidDataSource();
			druidMillis = testDataSource(testDataSource, "Druid", dataSource, testDAO, count);
			dataSource = null;
		}

 
		System.out.println("统计结果：[Druid]平均耗时" + druidMillis + "ms");

	}
 
	/**
	 * @param c3p0DataSource
	 * @param testDAO
	 * @param count
	 * @throws SQLException
	 * @throws InterruptedException
	 */
	private static long testDataSource(ITestDataSource testDataSource, String dataSourceName,
			DataSource c3p0DataSource, TestDAO testDAO, int count) throws SQLException, InterruptedException {
		System.out.println();
		System.out.println("查询次数为：" + count);
		System.out.println("==========================" + dataSourceName + " 测试开始==========================");
		// 测试
		// [预热数据库连接池] 查询10次以初始化连接池
		for (int i = 0; i < 10; i++) {
			testDAO.query(c3p0DataSource.getConnection());
		}

		long[] runMillis = new long[100];
		for (int i = runMillis.length - 1; i >= 0; i--) {
			runMillis[i] = testDataSource.queryDs(testDAO, c3p0DataSource, count);
			System.out.print("|" + i + "=" + runMillis[i] + "ms");
			if (i % 10 == 0) {
				System.out.println();
			}
		}
		// 开始统计平均值
		long avgMillis = 0l;
		for (int i = runMillis.length - 1; i >= 0; i--) {
			avgMillis += runMillis[i];
		}
		avgMillis = avgMillis / runMillis.length;
		System.out.println();
		System.out.println("==========================" + dataSourceName + " 测试结束==========================");
		System.out.println("平均耗时：" + avgMillis + "ms");
		return avgMillis;
	}
 
}

